package com.finalist.util;


import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;


public class HttpUtil {
	
	protected static Logger logger = Logger.getLogger(HttpUtil.class);
	private static final int SOCKET_TIME_OUT = 6000;
	private static final int CONNECT_TIME_OUT = 6000;
	
	private static CloseableHttpClient getClient() {
		CloseableHttpClient client = HttpClients.custom().build();
		
		return client;
	}
	
	private static RequestConfig getRequestConfig(int socketTimeout, int connectTimeout) {
		return RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
	}
	
	private static RequestConfig getRequestConfig() {
		return getRequestConfig(SOCKET_TIME_OUT, CONNECT_TIME_OUT);
	}
	
	private static void checkParam(String url, Map<String, String> param) {
		if(StringUtils.isNotBlank(url)) {
			String[] segs = url.split("\\?");
			if(segs.length > 1) {
				String[] segs2 = segs[1].split("&");
				if(segs2.length > 0) {
					for(String s : segs2) {
						String[] segs3 = s.split("=");
						if(segs3.length > 1) {
							param.put(segs3[0], segs3[1]);
						}
					}
				}
			}
		}
	}
	
	private static List<NameValuePair> createParam(Map<String, String> param) {
		List<NameValuePair> nvps = new ArrayList <NameValuePair>();
		if(param != null) {
			for(String k : param.keySet()) {
				nvps.add(new BasicNameValuePair(k, param.get(k)));
			}
		}
		
		return nvps;
	}
	
	private static HttpEntity sendGet(URI url) {
		CloseableHttpClient client = getClient();
		CloseableHttpResponse response = null;
		try {
			HttpGet req = new HttpGet(url);
			req.setConfig(getRequestConfig());
			
			response = client.execute(req);;
	        return response.getEntity();
		} catch(Exception e) {
			logger.error("sendGet error: ", e);
		} finally {
			if(response != null) {
				try {
					response.close();
					client.close();
				} catch(Exception ee) {
					logger.error("sendGet close error: ", ee);
				}
			}
		}
		
		return null;
	}
	
	/**
	 * 发送 get 请求
	 * @param url 请求地址，如 http://www.baidu.com
	 * @param param 相关参数, 会附在URL后面
	 * @return
	 */
	public static String get(String url, Map<String, String> param) {
		try {
			URIBuilder uri = new URIBuilder(url);
			param = param == null ? new HashMap<String, String>() : param;
			checkParam(url, param);
			uri.setParameters(createParam(param));
			
			HttpEntity entity = sendGet(uri.build());
			return EntityUtils.toString(entity, "UTF-8");
		} catch(Exception e) {
			logger.error("get error: ", e);
		}
		
		return null;
	}
	
	/**
	 * 发送 get 请求
	 * @param url 请求地址，如 http://www.baidu.com
	 * @return
	 */
	public static String get(String url) {
		return get(url, null);
	}
	
	/**
	 * 请求url，并且结果为InputStream
	 * @param url
	 * @return
	 */
	public static HttpEntity getAsStream(String url) {
		try {
			URIBuilder uri = new URIBuilder(url);
			HttpEntity entity = sendGet(uri.build());
			
			return entity;
		} catch(Exception e) {
			logger.error("getAsStream error: ", e);
		}
		
		return null;
	}
	
	private static HttpEntity sendPost(String url, HttpEntity entity) {
		CloseableHttpClient client = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		try {
			HttpPost req = new HttpPost(url);
			req.setConfig(getRequestConfig());
			req.setEntity(entity);
			
			response = client.execute(req);
		    return response.getEntity();
		} catch(Exception e) {
			logger.error("sendPost error: ", e);
		} finally {
			try {
				if(response != null) response.close();
				client.close();
			} catch(Exception ee) {
				logger.error("sendPost close error: ", ee);
			}
		}
		
		return null;
	}
	
	/**
	 * 发送  post 请求
	 * @param url 请求地址，如 http://www.baidu.com
	 * @param param相关参数, 模拟form 提交
	 * @return
	 */
	public static String postForm(String url, Map<String, String> param) {
		try {
			if(param != null) {
				HttpEntity entity = sendPost(url, new UrlEncodedFormEntity(createParam(param), Consts.UTF_8));
				return EntityUtils.toString(entity, "UTF-8");
			}
		} catch(Exception e) {
			logger.error("post error: ", e);
		} 
		
		return null;
	}
	
	/**
	 * 发送  post 请求
	 * @param url 请求地址，如 http://www.baidu.com
	 * @return
	 */
	public static String post(String url) {
		return postForm(url, null);
	}
	
	/**
	 * 发送  post 请求，并且结果为InputStream
	 * @param url
	 * @return
	 */
	public static HttpEntity postAsStream(String url) {
		try {
			HttpEntity entity = sendPost(url, null);
			return entity;
		} catch(Exception e) {
			logger.error("postAsStream error: ", e);
		} 
		
		return null;
	}
	
	/**
	 * 发送  post 请求
	 * @param url
	 * @param bodyText 写入到entity中
	 * @return
	 */
	public static String post(String url, String bodyText) {
		try {
			HttpEntity entity = sendPost(url, EntityBuilder.create().setContentType(ContentType.create("text/plain", "UTF-8")).setText(bodyText).build());
		    return EntityUtils.toString(entity, "UTF-8");
		} catch(Exception e) {
			logger.error("post error: ", e);
		} 
		
		return null;
	}

}
